/**
 * 
 */
package com.ffcs.crmd.tsp.server.netty.processor;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.client.TspJob;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.api.dto.page.RetVo;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.netty.ResponseCode;
import com.ffcs.crmd.tsp.core.util.BeanUtils;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.jdbc.entity.Job;
import com.ffcs.crmd.tsp.server.util.JobDetailHelper;
import com.ffcs.crmd.tsp.server.util.PublicMethodHelper;

import io.netty.channel.ChannelHandlerContext;

/**
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @since 
 *
 */
public class QueryJobProcessor implements INettyProcessor {
    /**
     * 日志
     */
    private final static Logger LOG = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    
    /**
     * 
     */
    private DataBaseHandler     dataBaseHandler;
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public QueryJobProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        this.dataBaseHandler = dataBaseHandler;
    }
    
    /**
     * 功能说明:
     * 
     * @param ctx
     * @param request
     * @return
     */
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        RemotingCommand response = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS);
        String requestXml = new String(request.getBody());
        try {
            LOG.debug("SERVER-NETTY: one request to query job is received that body=[{}]", requestXml);
            TspJob tspJobVO = (TspJob) XmlUtil.fromXml(requestXml, TspJob.class);
            RetVo<TspJob> tspJobRetVo = new RetVo<TspJob>();
            Job jobParam = new Job();
            BeanUtils.applyIf(jobParam, tspJobVO);
            if (PublicMethodHelper.isQueryPage(tspJobVO.getPageNumber(), tspJobVO.getPageSize())) {
                PageInfo<Job> jobPageInfo = dataBaseHandler.getServiceSupport().queryJobPage(jobParam);
                PageInfo<TspJob> tspJobPageInfo = new PageInfo<TspJob>();
                List<TspJob> tspJobList = new ArrayList<TspJob>();
                convert2JobVO(tspJobList, jobPageInfo.getList());
                BeanUtils.applyIf(tspJobPageInfo, jobPageInfo);
                tspJobPageInfo.setList(tspJobList);
                tspJobRetVo.setPageInfo(tspJobPageInfo);
            } else if (tspJobVO.getId() != null) {
                Job job = dataBaseHandler.getServiceSupport().getJob(jobParam);
                TspJob tspJob = convert2JobVO(job);
                tspJobRetVo.setObject(tspJob);
            } else {
                List<Job> jobList = dataBaseHandler.getServiceSupport().queryJob(jobParam);
                List<TspJob> tspJobList = new ArrayList<TspJob>();
                convert2JobVO(tspJobList, jobList);
                tspJobRetVo.setDataList(tspJobList);
            }
            response.setBody(XmlUtil.toXml(tspJobRetVo).getBytes());
        } catch (Exception e) {
            LOG.error("SERVER-NETTY: query job[{}] exception:", requestXml, e);
            String responseBody = String.format("query job:%s", e.getMessage());
            response.setCode(ResponseCode.FAIL);
            response.setBody(responseBody.getBytes());
        }
        return response;
    }
    
    private void convert2JobVO(List<TspJob> tspJobVOList, List<Job> jobList) throws Exception {
        for (Job job : jobList) {
            TspJob tspJob = convert2JobVO(job);
            tspJobVOList.add(tspJob);
        }
    }
    
    private TspJob convert2JobVO(Job job) throws Exception {
        TspJob tspJob = new TspJob();
        BeanUtils.applyIf(tspJob, job);
        JobDetailHelper.convert2JobVO(tspJob, job);
        return tspJob;
    }
    
}
